home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Snippets / Blasto-C / Blasto.c next >
Encoding:
C/C++ Source or Header  |  1995-02-09  |  3.9 KB  |  153 lines  |  [TEXT/MPS ]

  1. /*
  2.     Blasto
  3.     A small program that blasts an 8 bit color icon to the screen.
  4.     3-6-92         By Brigham Stevens
  5.                 Apple Developer Technical Support
  6.     3-13-92    - BRS - Uh Oh, it's Friday the 13th
  7.                 Cleaned up & commented code here and there.
  8.     4-13-92 - BRS - changed icon to be self-erasing
  9.                 Made code somewhat better by changing names and stuff.
  10.                 Added comments
  11.     4-24-92 - BRS - Added ifdefs around timing code, changed names again.
  12.     11-11-92 - BRS - made a THINK C project file, and made this code work with 
  13.                     THINK C.  Also changed the date in the comment above.
  14. */
  15.  
  16. /* Both Think C and MW C have precompiled headers.
  17. #ifndef THINK_C
  18. #include <OSUtils.h>
  19. #include <Windows.h>
  20. #include <QDOffscreen.h>
  21. #include <Retrace.h>
  22. #include <Memory.h>
  23. #include <Resources.h>
  24. #include <Events.h>
  25. #include <Menus.h>
  26. #endif*/
  27.  
  28. #include "DirectScreen.h"
  29.  
  30. void InitToolBox(short numMoreMasters);
  31.  
  32. /* Amount to move icons these need to be around 1 or 2    */
  33. /*    or our icon will not be self erasing */
  34. #define ROW_OFF    1
  35. #define COL_OFF 1
  36.  
  37. #define MAX_SECONDS 100
  38.  
  39. #ifndef THINK_C
  40. void ShowResult(long frameCount);
  41. long AverageTable(long *tab, short count);
  42. #endif
  43.  
  44. void main()
  45. {
  46.     WindowPtr        directWindow;
  47. //    GrafPort        killMenuBar;
  48.     Rect            screenRect;
  49.     GDHandle        mainScreen;
  50.     PixMapHandle    mainscreenPixMap;
  51.     Handle            colorIconHandle;
  52.     Point            plotLocation;
  53.     short            vDelta = ROW_OFF;
  54.     short            hDelta = COL_OFF;
  55.     long            frameCount;
  56. //    long            *ticks = (long*)0x16A;
  57.     long            beforeTicks, afterTicks;    
  58. //    long            tickTable[MAX_SECONDS];
  59. //    short            tickIndex = 0;
  60.     
  61.     long            result;
  62.     Str255            resultStr;
  63.  
  64.     /* Standard Witch Chant enclosed... */
  65.     InitGraf(&qd.thePort);
  66.     InitFonts();
  67.     InitWindows();
  68.     InitMenus();
  69.     InitCursor();
  70.     TEInit();
  71.     FlushEvents(everyEvent, 0);
  72.     InitDialogs(nil);
  73.     MoreMasters();
  74.     MoreMasters();
  75.     MaxApplZone();
  76. //    InitToolBox(4);        /* 4 calls to MoreMasters */
  77.  
  78.     /* get a handle to the color icon we are drawing */
  79.     colorIconHandle = GetResource('icl8',128);
  80.     if(!colorIconHandle) {
  81.         ParamText("\pBuild problems: 'icl8' not loaded.", "\p","\p","\p");
  82.         Alert(128, nil);
  83.         return;
  84.     }
  85.  
  86.     /* cover up the part of the screen we are writing on */
  87.     /* with a window, so other apps will not mess with our */
  88.     /* animation via update events in the background */
  89.     screenRect = qd.screenBits.bounds;
  90.     directWindow = NewWindow(nil,&screenRect,nil,true,plainDBox,nil,false,0L);
  91.  
  92.     /* This covers up the menu bar */
  93.     /* and fills the screen with white */
  94.     SetPort(directWindow);
  95.     RectRgn(directWindow->visRgn, &directWindow->portRect);
  96.     EraseRect(&directWindow->portRect);
  97.     
  98.     /* get the main screen's Pix map */
  99.     /* Which this program expects to be 8 bits deep */
  100.     mainScreen = GetMainDevice();
  101.     mainscreenPixMap = (**mainScreen).gdPMap;
  102.             
  103.     /* Set up a safe rectangle for bouncing off the screen */
  104.     screenRect.top = 4;
  105.     screenRect.left = 4;
  106.     screenRect.bottom -= 32;
  107.     screenRect.right -= 32;
  108.  
  109.     HideCursor();
  110.  
  111.     /* loop until the mouse is clicked */
  112.     /* drawing and moving the color icon */
  113.  
  114.     /* This is where to start drawing the color icon*/
  115.     plotLocation.h = 100;
  116.     plotLocation.v = 100;
  117.     frameCount = 0;
  118.     beforeTicks = TickCount(); //*ticks;
  119.  
  120.     while (!Button()) {
  121.         /* update the location of the icon */
  122.         plotLocation.v += vDelta;
  123.         plotLocation.h += hDelta;
  124.         /* This forces us to stay on the screen and prevent bus errors */
  125.         if(!PtInRect(plotLocation,&screenRect)) {
  126.             vDelta = -vDelta;    /* swap the deltas to bounce of corner */
  127.             hDelta = -hDelta;
  128.         }
  129.     
  130.         /* draw the icon */
  131.         
  132.         DirectPlotColorIcon((long *)*colorIconHandle, 
  133.                     mainscreenPixMap, plotLocation.v, plotLocation.h);
  134.         frameCount++;
  135.     }
  136.  
  137.     /* show the average number of frames per second for MAX_SECONDS seconds */
  138. #ifndef THINK_C    
  139. //    ShowResult( AverageTable(tickTable,MAX_SECONDS) );
  140. #endif
  141.     afterTicks = TickCount();
  142.  
  143.     ShowCursor();
  144.  
  145.     result = (frameCount * 60 / (afterTicks - beforeTicks));
  146.     NumToString(result, resultStr);
  147.     ParamText("\pFrames/sec:", resultStr, "\p", "\p");
  148.     Alert(128, nil);
  149.  
  150.     CloseWindow(directWindow);
  151.     DrawMenuBar();
  152.     FlushEvents(mDownMask, 0);
  153. }